package com.lib.framework.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import com.lib.framework.R;


/**
 * 未读红点 控件
 */
public class BadgeView extends AppCompatTextView {
    private boolean isPoint = false;

    public BadgeView(Context context) {
        super(context);
        setBackgroundResource(R.drawable.badge_bg);
        setGravity(Gravity.CENTER);
        setTextColor(WidgetUtil.getColorFromRes(R.color.white));
    }

    public BadgeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundResource(R.drawable.badge_bg);
        setGravity(Gravity.CENTER);
        setTextColor(WidgetUtil.getColorFromRes(R.color.white));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (isPoint) {
            super.onMeasure(View.MeasureSpec.makeMeasureSpec(WidgetUtil.dpTopx(getContext(), 10), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(WidgetUtil.dpTopx(getContext(), 10), View.MeasureSpec.EXACTLY));
        } else {
//            super.onMeasure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
//                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//
//            int height = getMeasuredHeight();
//            int width = getMeasuredWidth();
//            width = Math.max(height, width);
//
//            super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
//                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));

            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void showNumber(int number) {
        showNumber(number, 99);
    }

    //2019-09-09 18:42:35 样式修改，超过99显示...
    public void showNumberStr(int number) {
        setTypeface(Typeface.DEFAULT);
        if (number <= 0) {
            setVisibility(View.GONE);
        } else {
            if (number > 99) {
                setTypeface(Typeface.DEFAULT_BOLD);
                setText("···");
            } else {
                setText(String.valueOf(number));
            }
            setVisibility(VISIBLE);
        }

        int padding = (int) dpToPx(5.0f);
        setPadding(padding, 0, padding, 0);
        isPoint = false;
    }

    private float dpToPx(float dp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    public void showNumber(int number, int maxNumber) {
        setTypeface(Typeface.DEFAULT);
        if (number <= 0) {
            setVisibility(View.GONE);
        } else {
            if (number > maxNumber) {
                setText(String.valueOf(maxNumber) + "+");
            } else {
                setText(String.valueOf(number));
            }
            setVisibility(VISIBLE);
        }

        int padding = (int) dpToPx(5.0f);
        setPadding(padding, 0, padding, 0);
        isPoint = false;
    }

    public void showPoint() {
        setTypeface(Typeface.DEFAULT);
        setVisibility(View.VISIBLE);
        setText("");
        setPadding(0, 0, 0, 0);

        isPoint = true;
    }
}
